package com.bto.solar.work.service.biz_stage_handler;

import com.bto.solar.work.constant.BizStageEnum;
import com.bto.solar.work.constant.BizTaskStateEnum;
import com.bto.solar.work.constant.Constants;
import com.bto.solar.work.dao.IOrderStateService;
import com.bto.solar.work.service.BtoOrderService;
import com.bto.solar.work.service.BtoOrderStateService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

/**
 * @author 王小波
 * @description:
 * @date 2023/8/16 9:21
 */


@Slf4j
@Component
public class YeZhuStageHandler implements StageHandler{
    @Autowired
    private IOrderStateService orderStateService;
    @Autowired
    private BtoOrderStateService btoOrderStateService;
    @Autowired
    private BtoOrderService btoOrderService;

    @Override
    public void createNewInitState(String orderId, String stageId, String taskId) {
        btoOrderStateService.createOrderState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId() , null  , BizTaskStateEnum.WAITING_FILLED.getStateId());
    }

    @Override
    public void updateToWaitingApprovalState(String orderId, String stageId, String taskId) {
        btoOrderStateService.updateOrSaveOrderState(orderId, BizStageEnum.CUSTOMER_INFO.getStageId(), null,
                BizTaskStateEnum.WAITING_APPROVAL_INNER.getStateId(),
                BizTaskStateEnum.WAITING_FILLED.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT.getStateId(),
                BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId());
    }

    @Transactional
    @Override
    public void updateToApprovalPassState(String orderId, String stageId, String taskId  , String approvalType) {
        btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId() , null ,
                BizTaskStateEnum.APPROVAL_PASS.getStateId(),
                BizTaskStateEnum.WAITING_APPROVAL_INNER.getStateId()  , true);
        //推送越秀-进件风险审查
        btoOrderService.pushSyncProjectLeasePreApproval(orderId);
    }

    @Override
    public void updateToApprovalRejectState(String orderId, String stageId, String taskId , String approvalType , Object rejectInfo) {
        //越秀审核拒绝
        if(Constants.APPROVAL_LEASE.equals(approvalType)){
            btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId() , null , BizTaskStateEnum.APPROVAL_REJECT_LEASE.getStateId(),
                    BizTaskStateEnum.APPROVAL_PASS.getStateId() , true);
        }
        //内审审核拒绝
        else {
            btoOrderStateService.updateOrSaveOrderState(orderId , BizStageEnum.CUSTOMER_INFO.getStageId() , null , BizTaskStateEnum.APPROVAL_REJECT.getStateId(),
                    BizTaskStateEnum.WAITING_APPROVAL_INNER.getStateId() , true);
        }
    }
}
